/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseWorkFlowHistoryDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseWorkFlowHistoryDao : BaseDao, IBaseDao
    {
        public BaseWorkFlowHistoryDao()
        {
            base.CurrentTableName = BaseWorkFlowHistoryTable.TableName;
        }

        public BaseWorkFlowHistoryDao(IDbHelper dbHelper, BaseUserInfo userInfo)
        {
            base.DbHelper = dbHelper;
            base.UserInfo = userInfo;
        }

        public string AddEntity(BaseWorkFlowHistoryEntity workFlowHistoryEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseWorkFlowHistoryTable.TableName);
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseWorkFlowHistoryTable.TableName);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldID, sequence);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldStepID, workFlowHistoryEntity.StepID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldFlowID, workFlowHistoryEntity.FlowID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditUserID, workFlowHistoryEntity.AuditUserID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditUserFullName, workFlowHistoryEntity.AuditUserFullName);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditDepartmentID, workFlowHistoryEntity.AuditDepartmentID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditDepartmentFullName, workFlowHistoryEntity.AuditDepartmentFullName);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditIdea, workFlowHistoryEntity.AuditIdea);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditState, workFlowHistoryEntity.AuditState);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldSortCode, sequence);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldEnabled, workFlowHistoryEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldDescription, workFlowHistoryEntity.Description);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldCreateUserID, workFlowHistoryEntity.CreateUserID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldCreateUserFullName, workFlowHistoryEntity.CreateUserFullName);
            builder.SetDBNow(BaseWorkFlowHistoryTable.FieldCreateDate);
            return ((builder.EndInsert() > 0) ? sequence : string.Empty);
        }

        protected BaseWorkFlowHistoryEntity GetEntity(string objectID, string categoryID)
        {
            string sqlQuery = string.Empty;
            sqlQuery = " SELECT T.AUDITDepartmentFULLNAME  , T.AUDITSTAFFFULLNAME  , T.AUDITSTATE  ,T.AUDITIDEA  ,T.CREATEDATE  FROM " + BaseWorkFlowHistoryTable.TableName + " T  WHERE T.FLOWID =  (  SELECT W.ID  FROM T_WORKFLOWCURRENT W  WHERE W.OBJECTID = ?  AND W.CATEGORYID = ?  )  ORDER BY T.SORTCODE ";
            string[] targetFileds = new string[2];
            object[] targetValues = new object[2];
            targetFileds[0] = "ObjectID";
            targetFileds[1] = "CategoryID";
            targetValues[0] = objectID;
            targetValues[1] = categoryID;
            DataTable dataTable = new DataTable(BaseWorkFlowHistoryTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return new BaseWorkFlowHistoryEntity(dataTable);
        }

        public int UpdateEntity(BaseWorkFlowHistoryEntity workFlowHistoryEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseWorkFlowHistoryTable.TableName);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldStepID, workFlowHistoryEntity.StepID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldFlowID, workFlowHistoryEntity.FlowID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditUserID, workFlowHistoryEntity.AuditUserID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditUserFullName, workFlowHistoryEntity.AuditUserFullName);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditDepartmentID, workFlowHistoryEntity.AuditDepartmentID);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditDepartmentFullName, workFlowHistoryEntity.AuditDepartmentFullName);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditIdea, workFlowHistoryEntity.AuditIdea);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldAuditState, workFlowHistoryEntity.AuditState);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldSortCode, workFlowHistoryEntity.SortCode);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldEnabled, workFlowHistoryEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldDescription, workFlowHistoryEntity.Description);
            builder.SetValue(BaseWorkFlowHistoryTable.FieldModifyUserID, workFlowHistoryEntity.ModifyUserID);
            builder.SetDBNow(BaseWorkFlowHistoryTable.FieldModifyDate);
            builder.SetWhere(BaseWorkFlowHistoryTable.FieldID, workFlowHistoryEntity.ID);
            return builder.EndUpdate();
        }
    }
}